import tkinter as tk

import customtkinter as ctk
from pandastable import Table

from Attriabte import Color
from Entiy.WordGroup import WordGroup
from View.WindowComponent.BodyFrame.BodyFrame import BodyFrame
from View.WindowComponent.Layout.LayoutSide import LayoutSide


class EditWord(BodyFrame):
    def __init__(
            self,
            layout: LayoutSide,
            with_scoller=False,
            groupname=None,
            show_dataframe=None
    ):
        super().__init__(layout, with_scoller)
        self.groupname = groupname
        self.show_dataframe = show_dataframe
        self.wg = WordGroup()
    def pack(self):
        ctk.CTkLabel(
            self.frame,
            text=f"  | 管理 > 单词组 > {self.groupname if self.groupname is not None else self.show_dataframe.name}",
            text_font=('黑体', 20),
            anchor=tk.W
        ).place(
            width=700,
            height=140,
            x=0,
            y=0
        )

        if self.groupname:
            self.frmae_btn = ctk.CTkFrame(self.frame)
            btn1 = ctk.CTkButton(self.frmae_btn, text='保存修改', command=lambda :self.save_tabel())
            btn2 = ctk.CTkButton(self.frmae_btn, text='新增单词', command=lambda :self.add_tabel())
            btn3 = ctk.CTkButton(self.frmae_btn, text='重新读取', command=lambda :self.reload())
            btn4 = ctk.CTkButton(self.frmae_btn, text='重命名词组', command=lambda :self.rename())


            btn1.bg_color = 'white'
            btn1.grid(column=1, row=1, padx=5)
            btn2.bg_color = 'white'
            btn2.grid(column=2, row=1, padx=5)
            btn3.bg_color = 'white'
            btn3.grid(column=3, row=1, padx=5)
            btn4.grid(column=4, row=1, padx=5)

            self.frmae_btn.place(
                y=150,
                x=0,
                relwidth=1
            )

        framenew = ctk.CTkFrame(self.frame)
        framenew.place(
            y=200,
            x=0,
            relwidth=1
        )

        self.pt = Table(parent=framenew)
        if self.groupname is not None:
            self.pt.model.df = self.wg.load_group_dataframe(self.groupname)
        elif self.show_dataframe is not None:
            self.pt.model.df = self.show_dataframe
        print(self.pt.model.df.columns)
        self.pt.show()

    def save_tabel(self):
        df = self.pt.model.df
        self.wg.append_word(df, self.groupname)
        self.pt.model.df = self.wg.load_group_dataframe(self.groupname)

    def add_tabel(self):
        self.pt.addRows(1)

    def reload(self):
        self.layout.body(
            frame=EditWord(self.layout, False, self.groupname).get(), must_do=True
        )

    def rename(self):
        def _inner():
            nonlocal entry_name
            name = entry_name.get()
            self.wg.rename_group(self.groupname, name)
            entry_name.destroy()
            btn_name.destroy()


        entry_name = ctk.CTkEntry(self.frmae_btn, placeholder_text='词组名称')
        entry_name.grid(column=4, row=1, padx=5)

        btn_name = ctk.CTkButton(self.frmae_btn, text="确认", command=_inner)
        btn_name.grid(column=5, row=1, padx=5)

